//
//  PhotoListViewController.m
//  iFlickr
//
//  Created by 张 霄鹤 on 09-9-6.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "PhotoListViewController.h"

#import "PhotoInfo.h"

#define PERPAGE_IMAGE 9


@implementation PhotoListViewController

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewDidAppear:(BOOL)animated{
	[super viewDidAppear:animated];
	[self loadPhotosInPage:1];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
	
}



/**
 * 装载所有的图片列表数据
 *
 */
-(void)loadPhotoList:(NSArray *)ps{
	if(photos == nil){
		// 针对 photos 内容，经过测试发现 在 viewDidLoad中初始化，到这个方法中就会无法创建
		photos = [[NSMutableArray alloc] init];
	}
	[photos setArray:ps];
}

/**
 * 装载第几页 从 1 开始
 */
-(void)loadPhotosInPage:(NSInteger)page{
	int is = [photos count];
	if(is > 0 && page > 0){
		int minPage = is / PERPAGE_IMAGE;
		int mod = is % PERPAGE_IMAGE;
		int maxPage = minPage;
		if(mod != 0){
			maxPage += 1;
		}
		if(page <= maxPage){
			int startIndex = (page - 1) * PERPAGE_IMAGE;
			int endIndex = page * PERPAGE_IMAGE;
			NSLog(@"photos from %d to %d",startIndex, endIndex);
			PhotoInfo *pi;
			UIImage *img;
			NSInteger index = 0;
			for(int i = startIndex; i<endIndex && i < is; i++,index++){
				pi = (PhotoInfo *)[photos objectAtIndex:i];
				img = [self loadPhotoWithURL:[NSURL URLWithString:pi.url_sq]];
				[self setImage:index withImage:img];
			}
		}
	}
}


-(void)setImage:(NSInteger)index withImage:(UIImage *)img{
	switch (index) {
		case 0:
			img1.image = img;
			break;
		case 1:
			img2.image = img;
			break;
		case 2:
			img3.image = img;
			break;
		case 3:
			img4.image = img;
			break;
		case 4:
			img5.image = img;
			break;
		case 5:
			img6.image = img;
			break;
		case 6:
			img7.image = img;
			break;
		case 7:
			img8.image = img;
			break;
		case 8:
			img9.image = img;
			break;
		default:
			break;
	}
}

/**
 * 重置所有图像
 */
-(void)resetImage{
	img1.image = nil;
	img2.image = nil;
	img3.image = nil;
	img4.image = nil;
	img5.image = nil;
	img6.image = nil;
	img7.image = nil;
	img8.image = nil;
	img9.image = nil;
}

- (void)dealloc {
	if(photos){
		NSLog(@"dealloc ");
		[photos removeAllObjects];
		[photos release];
	}
    [super dealloc];
}


@end
